package com.cysyz.commonservice.mycommonservice;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.cysyz.mylib.dao.BaseDao;
import org.apache.commons.beanutils.BeanUtils;

import com.cysyz.commonservice.mycommonservice.exception.InputValueIsNullOrEmptyException;
import com.cysyz.commonservice.mycommonservice.exception.ValueNotMatchException;
import com.cysyz.mylib.dao.Conds;
import com.cysyz.utils.MD5;

public abstract class BaseService{
	@Resource
	protected BaseDao dao;

	public void addNew(Object entity) {
		dao.save(entity);
	}
	/**
	 * special entity is exist
	 * 
	 * @param clazz
	 * @param id
	 * @return
	 */
	public <T> boolean isExist(Class<T> clazz, String id) {
		Conds conds = new Conds();
		conds.addFilter(clazz.getSimpleName(), "id", id);
		return dao.getCount(clazz, conds) > 0;
	}

	public void checkNullOrEmpty(String value, String message)
			throws InputValueIsNullOrEmptyException {
		if (value instanceof String) {
			if (value == null || value.isEmpty()) {
				throw new InputValueIsNullOrEmptyException(message);
			}
		}
	}

	public void checkNullOrEmpty(Object entity, String... fieldNames)
			throws InputValueIsNullOrEmptyException {
		List<String> isNullOrEmptyFields = new ArrayList<String>();
		for (String fieldName : fieldNames) {
			try {
				String value = BeanUtils.getProperty(entity, fieldName);
				if (value == null) {
					isNullOrEmptyFields.add(fieldName);
					continue;
				} else if (value instanceof String && ((String) value).isEmpty()) {
					isNullOrEmptyFields.add(fieldName);
					continue;
				}
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}
		if (!isNullOrEmptyFields.isEmpty()) {
			throw new InputValueIsNullOrEmptyException(isNullOrEmptyFields);
		}
	}

	public static void checkValueEqual(Object entityInput, Object entityDB,
			String... fieldNames) throws ValueNotMatchException {
		String entityInputFieldName = null;
		String entityDBFieldName = null;
		List<String> notequalEntityInputFieldNames = new ArrayList<String>();
		for (String fieldName : fieldNames) {
			if (fieldName.contains(":")) {
				String[] twoPart = fieldName.split(":");
				entityInputFieldName = twoPart[0];
				entityDBFieldName = twoPart[1];
			} else {
				entityInputFieldName = fieldName;
				entityDBFieldName = fieldName;
			}
			try {
				if (!isEqual(entityInput, entityDB, entityInputFieldName,
						entityDBFieldName)) {
					notequalEntityInputFieldNames.add(entityInputFieldName);
				}
			} catch (IllegalAccessException | InvocationTargetException
					| NoSuchMethodException e) {
				throw new RuntimeException(e);
			}
		}
		if (!notequalEntityInputFieldNames.isEmpty()) {
			throw new ValueNotMatchException(notequalEntityInputFieldNames);
		}
	}

	private static boolean isEqual(Object entityInput, Object entityDB,
			String entityInputFieldName, String entityDBFieldName)
			throws IllegalAccessException, InvocationTargetException,
			NoSuchMethodException {
		String input = BeanUtils.getProperty(entityInput, entityInputFieldName);
		if (entityInputFieldName.equals("password")) {
			input = MD5.MD5Encode(input);
		}
		String db = BeanUtils.getProperty(entityDB, entityDBFieldName);
		return input.equals(db);
	}
	
	/*public static void main(String[] args) {
		User user = new User();
		user.setId("user_resetps");
		user.setPassword("12");
		user.setResetPsMD5("f38f09944991aff48b78c0a777f730ba");
		User user1 = new User();
		user1.setId("user_resetps");
		user1.setPassword("202cb962ac59075b964b07152d234b70");
		user1.setResetPsMD5("f38f09944991aff48b78c0a777f730ba");
			try {
				BaseService.checkValueEqual(user, user1, "password");
			} catch (ValueNotMatchException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}*/
	
}
